/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.util;

import org.openislands.oi.constant.DataFormat;
import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.constant.ModelFormat;
import org.openislands.oi.constant.StorageEngine;
import org.openislands.oi.error.LogicException;
import org.openislands.oi.pojo.dto.OfNodeTaskDTO;
import java.nio.file.Paths;

public class IOUtils {
    public static String genDataIOUri(OfNodeTaskDTO ofNodeTaskDTO, String outputName, DataFormat format, StorageEngine storageEngine){
        if (storageEngine == StorageEngine.LOCALFS){
            return StorageEngine.LOCALFS.getDesc() + Paths.get(DirectoryUtils.getProjectRootPath().toString(), "localfs", genDataIOPath(ofNodeTaskDTO, outputName, format));
        }else {
            throw new LogicException(MessageCodeEnum.NOT_SUPPORT_ERROR, String.format("not support %s storage engine", storageEngine.name()));
        }
    }

    public static String genModelIOUri(OfNodeTaskDTO ofNodeTaskDTO, String outputName, ModelFormat format, StorageEngine storageEngine){
        if (storageEngine == StorageEngine.LOCALFS){
            return StorageEngine.LOCALFS.getDesc() + Paths.get(DirectoryUtils.getProjectRootPath().toString(), "localfs", genModelIOPath(ofNodeTaskDTO, outputName, format));
        }else {
            throw new LogicException(MessageCodeEnum.NOT_SUPPORT_ERROR, String.format("not support %s storage engine", storageEngine.name()));
        }
    }

    public static String genDataIOPath(OfNodeTaskDTO ofNodeTaskDTO, String outputName, DataFormat format){
        return Paths.get(DirectoryUtils.genTaskOutputDataDirPath(ofNodeTaskDTO).toString(), String.format("%s.%s", outputName, format.name().toLowerCase())).toString();
    }

    public static String genModelIOPath(OfNodeTaskDTO ofNodeTaskDTO, String outputName, ModelFormat format){
        return Paths.get(DirectoryUtils.genTaskOutputModelDirPath(ofNodeTaskDTO).toString(), String.format("%s.%s", outputName, format.name().toLowerCase())).toString();
    }
}
